package com.elm.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.elm.dao.UserOrderDao;
import com.elm.model.UserOrder;
import com.elm.util.JDBCUtil;
/**

 * 

 * @author pgm

 * 

 */
public class UserOrderDaoImpl implements UserOrderDao{

	@Override
	public List<UserOrder> getAllPastOrder(int userid, int userorderstate) {
		List<UserOrder> uorders = new ArrayList<UserOrder>();
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from userorder where userid = ? and userorderstate = ?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, userid);
			pst.setInt(2, userorderstate);
			ResultSet rs = pst.executeQuery();
			while(rs.next()) {
				UserOrder u = new UserOrder();
				u.setCarid(rs.getString("carid"));
				u.setUserid(userid);
				u.setUserorderstate(userorderstate);
				u.setUserorderid(rs.getInt("userorderid"));
				uorders.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block


			e.printStackTrace();
		}
		return uorders;
	}
	@Override
	public List<UserOrder> getAllOrders() {
		List<UserOrder> alluserorders = new ArrayList<UserOrder>();
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select * from userorder");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()) {
				UserOrder u = new UserOrder();
				u.setCarid(rs.getString("carid"));
				u.setUserid(rs.getInt("userid"));
				u.setUserorderstate(rs.getInt("userorderstate"));
				u.setUserorderid(rs.getInt("userorderid"));
				alluserorders.add(u);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return alluserorders;
	}

	@Override
	public void insertOrder(String carid, int userorderstate, int userid) {
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("insert into userorder(carid, userorderstate, userid) values(?, ? , ?)");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, carid);
			pst.setInt(2, userorderstate);
			pst.setInt(3, userid);
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public String getCaridByUserOrderId(int userorderid) {
		String carid = "";
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select carid from userorder where userorderid = ?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, userorderid);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				carid = rs.getString("carid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return carid;
	}
	@Override
	public int updateUserOrderState(int state, int userorderid) {
		int i = 0;
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("update userorder set userorderstate = ? where userorderid = ?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, state);
			pst.setInt(2, userorderid);
			i = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	@Override
	public int removeUserOrderState(int userorderid) {
		int i = 0;
		Connection con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("update userorder set userorderstate = 3 where userorderid = ? ");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, userorderid);
			i = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	
}
